package com.spice.service.entity.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.spice.common.constant.PermissionConstant;
import com.spice.common.model.BaseEntity;
import com.spice.common.util.CheckUtil;
import com.spice.common.util.MD5;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.PastOrPresent;

@Data
//@JsonInclude(JsonInclude.Include.NON_NULL)
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
@ApiModel(description = "用户信息")
public class User extends BaseEntity {
    @ApiModelProperty(value = "昵称", example = "殷海铭")
    private String nickname;

    @ApiModelProperty(value = "头像", example = "https://obuivy.oss-cn-shanghai.aliyuncs.com/logo.png")
    private String avatar;

    @ApiModelProperty(value = "状态", example = "none:user")
    private String status;

    @ApiModelProperty(value = "登录用户名", example = "root")
    private String username;

    @ApiModelProperty(value = "加密过的登录密码", example = "yhm20010921")
    private String password;

    @PastOrPresent
    @ApiModelProperty(value = "上次登录时间", example = "2021-06-28 20:48:20")
    private String lastLoginTime;


    /**
     * 确认该user的用户名和密码是否符合规范
     * @return 是否符合
     */
    public boolean checkUsernameAndPassword() {
        return checkUsername() && checkPassword();
    }

    /**
     * 确认该user的用户名是否符合规范
     * @return 是否符合
     */
    public boolean checkUsername() {
        return CheckUtil.checkUsername(this.username);
    }

    /**
     * 确认该user的密码是否符合规范
     * @return 是否符合
     */
    public boolean checkPassword() {
        return CheckUtil.checkPassword(this.password);
    }

    /**
     * 加密密码
     * @return 本对象
     */
    public User encryptPassword() {
        this.password = MD5.encrypt(this.password);
        return this;
    }

    /**
     * 清理禁止修改的数据成员
     * @return 本对象
     */
    @Override
    public User cleanup() {
        // 新增加的用户只有user
        lastLoginTime = null;
        super.cleanup();
        return this;
    }
    /**
     * 移除敏感信息
     * @return 本对象
     */
    public User desensitization(){
        password = null;
        return this;
    }

}
